package com.foreknow.demo18;

import com.foreknow.demo12.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * jdbc的工具类
 */
public class DBTools {
    //获取对象
    private Connection conn;
    //获取预处理
    private PreparedStatement pstmt;
    private ResultSet rs;
    private int isRight;
    /**
     * 创建连接
     */
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        //1.加载驱动程序
        //如果是8.0以上的版本需要使用com.mysql.cj.jdbc.Driver
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        //8.0以上: jdbc:mysql://localhost:3306/basic2?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
        //8.0以下版本：jdbc:mysql://localhost:3306/basic2
         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic2","root","123456");
        return conn;
    }
    /**
     * 查询的方法
     */
    /**
     * 传语句和数组
     * @param sql
     * @param values
     */
    public ResultSet getQuery(String sql,Object...values) throws SQLException {
       pstmt= conn.prepareStatement(sql);
       for (int i=0;i<values.length;i++){
           //将? 替换一个具体的值
           pstmt.setObject(i+1,values[i]);
       }
        rs = pstmt.executeQuery();

       return rs;
    }
//    public List<Statement> add(Statement statement) throws SQLException {
//        //获取statement对象
//        Statement stmt =conn.createStatement();
//        //查询
//          ResultSet rs = stmt.executeQuery("select * from emp");
//        List<Statement> list = new ArrayList<>();
//
//        list.add(statement);
//        //获取结果集的数据 next()方法是将光标向下移动一行,获取当前行的数据
//          while (rs.next()){
//              int empno =rs.getInt("empno");
//              String ename=rs.getNString("ename");
//              String job = rs.getString("job");
//              Date hir = rs.getDate("hiredate");
//              System.out.println(empno+"   "+ename+"   "+"   "+job);
//          }
//          return  list;
//    }
    /**
     * DML操作
     */
    public int Dml(String sql,Object...values) throws SQLException {
        //获取预处理
        pstmt= conn.prepareStatement(sql);
        for (int i=0;i<values.length;i++){
            //将? 替换一个具体的值
            pstmt.setObject(i+1,values[i]);
        }
       int a =pstmt.executeUpdate();
        return a;
    }

    /**
     * 释放资源
     */
    /**
     * 释放资源
     */
    public void closed() throws SQLException {
        if(rs!=null) {
            rs.close();
        }
        if(pstmt!=null) {
            pstmt.close();
        }
        if(conn!=null) {
            conn.close();
        }
    }
    public static void main(String[] args) {
        /**
         * 传语句和数组
         * @param sql
         * @param values
         */
        DBTools dbTools = new DBTools();
//        try {
//            //链接数据库
//            dbTools.getConnection();
//          int is=  dbTools.Dml("update emp set ename ='ooo' where empno=1001");
//            System.out.println(is);
//            //释放资源
//            dbTools.closed();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
        try {
           dbTools.getConnection();
            ResultSet rs =dbTools.getQuery("select * from emp where empno=?" ,8888);

            while (rs.next()){
              int empno =rs.getInt("empno");
              String ename=rs.getNString("ename");
              String job = rs.getString("job");
              Date hir = rs.getDate("hiredate");
              System.out.println(empno+"   "+ename+"   "+"   "+job);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        /**
         * 创建连接
         */
//        DBTools dbTools = new DBTools();
//        try {
//          Connection connection=  dbTools.getConnection();
//
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
    }
}
